#include<bits/stdc++.h>
#define ll long long
using namespace std;
//Printing statement
#define el endl
#define printArray(v) for(auto k:v)cout<<k<< <<;
//STL operation
#define pb push_back
#define in insert
#define all(v) v.begin(),v.end()
#define asc(v) sort(all(v))
#define dsc(v) asc(v),reverse(all(v))
#define AvaDe ios_base::sync_with_stdio(false), cin.tie(NULL),cout.tie(NULL);
// BAKI SAB THEEK BAS CHAL RAHA HAI BRO...
bool comp(string a, string b)
{
if (a + b < b + a)
return true;
else
return false;
}
int prime(vector<int> v, int n)
{
int ct = 0;
vector<int> vv(n, true);
for (int i = 2; i <= n; i++)
{
if (vv[i] == true)
ct++;
for (int j = 2 * i; j <= n; j = j + i)
{
vv[j] = false;
}
}
return ct;
// it returns only count.
}
bool comper(pair<int,int>&p1,pair<int,int>&p2)
{
//Return it in the same order in which it's required
return p1.second<p2.second;
}
int findLastIndex(string& str, char x,int ind)
{
int index = ind;
for (int i = ind; i < str.length(); i++)
if (str[i] == x)
index = i;
if(ind==str.size()-1){
return -1;
}
else{
return index;
}
}
void solve(){
string s;
cin>>s;
if(s.size()==1)
{
cout<<s;
return;
}
set<char>s1;
vector<char>v;
for (size_t i = 0; i < s.size(); i++)
{
s1.insert(s[i]);
}
for(auto i:s1){
v.pb(i);
}
reverse(v.begin(),v.end());
int j=0;
string ans="";
int ind=0;
set<int>vv;
while(findLastIndex(s,v[j],ind)!=-1)
{
int q=findLastIndex(s,v[j],ind);
j++;
ind=q;
vv.insert(q);
}
vector<int>v2;
for(auto i:vv){
v2.pb(i);
}
int ct=0;
// for(auto i:v2){
// cout<<i<<" ";
//
map<char,int>m;
char ch='a';
for(int j=0;j<=v2[0];j++)
{
m[s[j]]++;
ch=max(ch,s[j]);
}
// cout<<ch;
for(int k=0;k<m[ch];k++)
{
ans+=ch;
}
ct++;
// cout<<ans<<el;
// cout<<" hi";
for(int i=0;i<v2.size()-1;i++)
{
map<char,int>mp;
char ch='a';
// else{
for(int j=v2[i]+1;j<=v2[i+1];j++)
{
mp[s[j]]++;
ch=max(ch,s[j]);
}
for(int k=0;k<mp[ch];k++)
{
ans+=ch;
}
// }
// for(auto i:mp)
// cout<<i.first<<" "<<i.second<<el;
}
// for(auto i:vv)
// cout<<i<< " "<<el;
// cout<<s.size();
cout<<ans;
}
int main()
{
AvaDe
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Two Strings | Anagrams |
Prime Number | Lexical Sorting Reloaded |
1514A - Perfectly Imperfect Array | 580A- Kefa and First Steps |
1472B- Fair Division | 996A - Hit the Lottery |
MSNSADM1 Football | MATCHES Playing with Matches |
HRDSEQ Hard Sequence | DRCHEF Doctor Chef |
559. Maximum Depth of N-ary Tree | 821. Shortest Distance to a Character |
1441. Build an Array With Stack Operations | 1356. Sort Integers by The Number of 1 Bits |
922. Sort Array By Parity II | 344. Reverse String |
1047. Remove All Adjacent Duplicates In String | 977. Squares of a Sorted Array |
852. Peak Index in a Mountain Array | 461. Hamming Distance |
1748. Sum of Unique Elements | 897. Increasing Order Search Tree |
905. Sort Array By Parity | 1351. Count Negative Numbers in a Sorted Matrix |
617. Merge Two Binary Trees | 1450. Number of Students Doing Homework at a Given Time |
700. Search in a Binary Search Tree | 590. N-ary Tree Postorder Traversal |